// 导入资源
import { ElMessage } from 'element-plus'
import { ref } from 'vue'
import router from '@/router/index.ts'
import { register } from '@/api/user.js'

// 定义变量
const username = ref('')
const passwords = ref('')
const passwordsTwo = ref('')
const iphonenum = ref('')
const bodyStyle = ref({
  padding: '50px 20px 20px 20px'
})
// 主方法
export default function getUnloginFunction () {
  function onSubmit () {
    if (passwords.value === passwordsTwo.value && iphonenum.value !== '' && username.value !== '') {
      register({
        iphonenum: iphonenum.value,
        username: username.value,
        passwords: passwords.value
      }).then((res) => {
        if (res.data.massage === true) {
          ElMessage.success({
            message: '注册成功',
            type: 'success'
          })
          setTimeout(() => {
            router.push('/login')
          }, 1100)
        } else {
          ElMessage.error('此账号已经注册')
        }
      })
    } else if (iphonenum.value === '' || passwords.value === '' || username.value === '' || passwordsTwo.value === '') {
      ElMessage.error('请补全信息')
    } else {
      ElMessage.error('注册失败两次输入的密码不一致')
    }
  }
  function backlogin () {
    router.push('/login')
  }
  return {
    username,
    passwords,
    bodyStyle,
    iphonenum,
    passwordsTwo,
    onSubmit,
    backlogin
  }
}
